﻿@page "/employee"
@using System.Collections.ObjectModel
@inject DialogService DialogService
@inject IRBACUserService RBACService

<RadzenText TextStyle="TextStyle.H4">员工管理</RadzenText>
<PageTitle>员工管理</PageTitle>
<RadzenStack Style="display: flex;" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Start" AlignItems="AlignItems.Center">
    <RadzenTextBox @bind-Value="@keyword" Placeholder="账号/姓名" />
    <RadzenButton Icon="refresh" Click=@(args => Search()) ButtonStyle="ButtonStyle.Primary"/>
    <RadzenButton Icon="add_circle" Click=@(args => Add()) ButtonStyle="ButtonStyle.Primary"/>
</RadzenStack>
<RadzenDataGrid EmptyText="暂无数据" TItem="UserDto" Data="@employees" GridLines="Radzen.DataGridGridLines.Default" AllowPaging="true" AllowSorting="true">
    <Columns>
        <RadzenDataGridColumn Property="" Title="操作" Width="150px" MinWidth="20px" MaxWidth="150px">
            <Template Context="data">
                <RadzenStack Gap="10" Style="margin-bottom: 10px" Orientation="Orientation.Horizontal">
                    <RadzenButton Icon="edit" @onclick:stopPropagation Click="@(args=> Update(data))" ButtonStyle="ButtonStyle.Primary" />
                    <RadzenButton Icon="delete" @onclick:stopPropagation Click="@(args=> Delete(data))" ButtonStyle="ButtonStyle.Danger" />
                    <RadzenButton Icon="report" @onclick:stopPropagation Click="@(args=> SetRole(data))" ButtonStyle="ButtonStyle.Success" />
                </RadzenStack>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="Account" Title="账号" />
        <RadzenDataGridColumn Property="Name" Title="姓名" />
        <RadzenDataGridColumn Property="Phone" Title="电话" />
        <RadzenDataGridColumn Property="Remark" Title="备注" />
    </Columns>
</RadzenDataGrid>

@code {
    string keyword = string.Empty;
    ObservableCollection<UserDto> employees = new ObservableCollection<UserDto>();

    async Task Search()
    {
        var list = await RBACService.GetListAsync("");
        employees = new ObservableCollection<UserDto>(list);
    }

    private async void Add()
    {
        var newUser = new UserRegisterDto();
        var userdto = await DialogService.OpenAsync<UserAddDialog>(
            $"新增员工",
            new Dictionary<string, object>() {{ "PUser", newUser}},
            new DefaultDialogOptions());

        if(userdto != null)
        {
            employees!.Add(userdto);
        }
    }

    private async void Update(UserDto selectedItem)
    {
        UserDto user = await DialogService.OpenAsync<UserAddDialog>($"修改员工",
                      new Dictionary<string, object>(){{"PUser", selectedItem}},
                      new DefaultDialogOptions());
        if (user != null)
        {
            selectedItem.Account = user.Account;
            selectedItem.Name = user.Name;
            selectedItem.Phone = user.Phone;
            selectedItem.Remark = user.Remark;
            StateHasChanged();
        }
    }

    // todo : 需要传递用户信息
    private async void Delete(UserDto selectedUser)
    {
        var result = await DialogService.Confirm("确认要删除吗?", "提示", new ConfirmOptions() 
       { 
           OkButtonText = "确定", 
           CancelButtonText = "取消" 
       });

        if ((bool)result)
        {
            try
            {
                await RBACService.Delete(selectedUser.Id);
                employees!.Remove(selectedUser);
            }
            catch (Exception e)
            {
                await DialogService.Alert(e.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
            }
        }
    }

    // todo : 需要传递用户信息
    private async Task SetRole(UserDto user)
    {
        await DialogService.OpenAsync<UserAssignRoleDialog>($"分配角色",
            new Dictionary<string, object>() { { "PUser", user } },
            new DialogOptions()
                {
                    Resizable = true,
                    Draggable = true,
                    Width = "700px",
                    Height = "512px",
                    Left = null,
                    Top = null
                });
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await Search();
    }

}
